package com.secret.chain.secretchain.controller;

import com.secret.chain.secretchain.BO.AddressBO;
import com.secret.chain.secretchain.BO.AnswerBO;
import com.secret.chain.secretchain.utils.ContractServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigInteger;

@Controller
@RequestMapping("/demo")
public class demoController {

    @Autowired
    ContractServiceImpl contractService;

    @GetMapping("/test")
    public AddressBO test() throws ContractException {
        //创建合约
        String address="";
        //创建 问题
        AnswerBO answerBO = new AnswerBO(BigInteger.valueOf(1), "yangyang", BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(1));
        //设置问题  部署合约
        AddressBO addressBO = contractService.setAnswer(address, answerBO);

        //通过地址值拿到 问题
        AnswerBO answer = contractService.getAnswer(addressBO.getAddress());
        System.out.println(answer.getContent());
        System.out.println(addressBO.getAddress());


        //改变内容
        answer.setContent("yangxiangeui");
        //修改问题  重新部署合约，更新合约
        AddressBO addressBO1 = contractService.setAnswer(addressBO.getAddress(), answer);
        boolean empty = StringUtils.isEmpty(address);


        System.out.println(answer.getContent());
        System.out.println(addressBO.getAddress());


        //返回地址存储到数据库中
        return  addressBO;
    }


}
